package com.zxs.shopping;

import java.util.Scanner;

/**
 * 主函数
 *
 * @author lenovo
 * @since 2024/11/30 21:25
 */
public class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int y = 0;
    int x = 0;
    System.out.println("商品有：苹果，香蕉，橙子，牛奶，可乐，雪碧，方便面，薯片，巧克力\n输入名称查看具体信息，输入'退出'结束");
    Product[] products = new Product[9];
    Product[] shoppingcart = new Product[9];
    products[0] = new Product("苹果", 2.5, 100, "水果");
    products[1] = new Product("香蕉", 1.5, 100, "水果");
    products[2] = new Product("橙子", 3.5, 100, "水果");
    products[3] = new Product("牛奶", 5.0, 100, "饮料");
    products[4] = new Product("可乐", 3.0, 100, "饮料");
    products[5] = new Product("雪碧", 3.0, 100, "饮料");
    products[6] = new Product("方便面", 4.0, 100, "零食");
    products[7] = new Product("薯片", 2.0, 100, "零食");
    products[8] = new Product("巧克力", 10.0, 100, "零食");
    while (true) {
        String input = scanner.nextLine();
        if (input.equals("退出")) {
            break;
        }
        for (int i = 0; i < 9; i++) {
            if (products[i].getName().equals(input)) {
                y = 1;
                System.out.println("商品名称：" + products[i].getName());
                System.out.println("价格：" + products[i].getPrice());
                System.out.println("数量：" + products[i].getConut());
                System.out.println("类型：" + products[i].getType());
                System.out.println("输入‘1’添加到购物车,输入‘0’继续选择商品");
                int a = scanner.nextInt();
                if (a == 1) {
                    System.out.println("请输入购买数量：");
                    int count = scanner.nextInt();
                    if (count > products[i].getConut()) {
                        System.out.println("库存不足，重新选购");
                    } else {
                        shoppingcart[x] = products[i];
                        shoppingcart[x].setConut(count);
                        x++;
                        System.out.println("添加成功,继续选购");
                    }
                }
                if (a == 0) {
                    System.out.println("继续选购");
                }
            }
        }
        if (y == 0) {
            System.out.println("没有此商品");
        }
    }
    int p = x;
    while (true) {
        System.out.println("输入‘0’删除商品，输入‘1’查看购物车，输入‘2’结算");
        int b = scanner.nextInt();
        if (b == 1) {
            for (int i = 0; i < p; i++) {
                if (shoppingcart[i] != null) {
                    System.out.println("商品名称：" + shoppingcart[i].getName() + " " + "价格" + shoppingcart[i].getPrice() + " " + "数量" + shoppingcart[i].getConut() + " " + "类型" + shoppingcart[i].getType());
                }
            }
        }
        if (b == 0) {
            System.out.println("输入要删除的商品名称");
            String name = scanner.next();
            for (int i = 0; i < p; i++) {
                if (shoppingcart[i]!=null&&shoppingcart[i].getName().equals(name)) {
                    for(int j = i; j < p - 1; j++) {
                        shoppingcart[j] = shoppingcart[j + 1];
                    }
                    p = p - 1;
                    System.out.println(p);
                    System.out.println("删除成功");
                }
            }
        }
        float num = 0, a;
        if (b == 2) {
            for (int i = 0; i < p; i++) {
                if (shoppingcart[i] != null) {
                    a = (float) (shoppingcart[i].getConut() * shoppingcart[i].getPrice());
                    num = num + a;
                }
            }
            System.out.println("总价为" + num);
            break;
        }
    }
}
}